package com.sky.controller.user;

import com.sky.dto.UserLoginDTO;
import com.sky.entity.User;
import com.sky.result.Result;
import com.sky.service.UserService;
import com.sky.utils.JwtUtils;
import com.sky.vo.UserLoginVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/user/user")
@Slf4j
@Tag(name = "用户接口")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("微信用户登录：{}",userLoginDTO);
        User user = userService.wxLogin(userLoginDTO);
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("userId",user.getId());
        String jwt = JwtUtils.createJwt(claims);
        log.info("生成的jwt:{}",jwt);
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .openId(user.getOpenId())
                .authentication(jwt)
                .build();
        return Result.success(userLoginVO);
    }
}
